package PendulumPackage model Plant parameter Modelica.SIunits.Angle theta_start = 0.1; parameter Modelica.SIunits.Mass M = 2 "Cart mass"; parameter Modelica.SIunits.Mass m = 0.1 "Pendulum mass"; parameter Modelica.SIunits.Length l = 0.5 "Pendulum lenght"; Modelica.SIunits.Position x; Modelica.SIunits.Velocity x_dot; Modelica.SIunits.Angle theta(start = theta_start); Modelica.SIunits.AngularVelocity theta_dot; Modelica.SIunits.Force f; equation der(x) = x_dot; der(theta) = theta_dot; M * l * der(theta_dot) = (M + m) * 9.81 * theta - f; M * der(x_dot) = f - m * 9.81 * theta; assert(M > 0 and m > 0 and l > 0, "Plant outside of the domain of validity", AssertionLevel.error); end Plant; model Controller extends Plant; constant Real[4] kF = {0.1020408,0.4081633,26.63102,4.2040816}; constant Real[4] kC = {417.95918,208.97954,613.55954,136.4898}; discrete Real u; discrete Boolean modeFine(start = true) "discrete state"; discrete Boolean modeChange = false "External event"; equation when sample(0, 0.05) then if modeChange then modeFine = not pre(modeFine); else modeFine = pre(modeFine); end if; if modeFine then u = x * kF[1] + x_dot * kF[2] + theta * kF[3] + theta_dot * kF[4]; else u = x * kC[1] + x_dot * kC[2] + theta * kC[3] + theta_dot * kC[4]; end if; f = u; end when; end Controller; end PendulumPackage;